<?php
header("Content-type:text/html; charset=utf-8");
require_once ('config.php');
require_once (WEBURL.'/include/conn.php');
require_once (WEBURL.'/login_status_check.php');
if(!isset($_POST['channel_name'])){
	echo "forbidden!";
	exit();
}
$pre_channel=$_POST['pre_id'];
if(!checkLogin()){
	$gotopage=base64_encode("channel_add.php?pre_id=$pre_channel");
	header("Location:admin_login.php?gotopage=".$gotopage);
}
else{
	$channel_name=$_POST['channel_name'];
	if($pre_channel==-1){
		$pre_channel=$_POST['pre_channel'];
	}
	$channel_grade=$_POST['pre_channel_grade']+1;
	$whether_channel_has_articles=$_POST['whether_channel_has_articles'];
	$article_move_to_channel=$_POST['article_move_to_channel'];
	$channel_model=$_POST['channel_model'];
	$article_model=$_POST['article_model'];
	$channel_describe=base64_encode($_POST['channel_describe']);

	$sql_channel_add_save="insert into channel(pre_id,channel_grade,channel_name,channel_visit_path,channel_describe,is_bottom,channel_model,article_model)
	values($pre_channel,$channel_grade,'$channel_name',' ','$channel_describe',1,'$channel_model','$article_model');";
	//echo $sql_channel_add_save;
	$result_channel_add_save=@mysql_query($sql_channel_add_save);
	if($result_channel_add_save){
              /*获取新添加栏目的id*/
		        $sql_get_max_channel_id="select MAX(channel_id) from channel;";
				//echo $sql_get_max_channel_id;
				$result_get_max_channel_id=mysql_query($sql_get_max_channel_id) or die(mysql_error());
				$row_get_max_channel_id=mysql_fetch_row($result_get_max_channel_id);
			    $channel_id=$row_get_max_channel_id[0];
		/*若父栏目中有文章，将这些文章进行转移操作*/
		if($whether_channel_has_articles==1){
			if($article_move_to_channel==0){
					$article_move_to_channel=$channel_id;
			}
			/*替换，数据库操作*/
			$sql_update_article_channel="update article_normal set article_main_channel=$article_move_to_channel where article_main_channel=$pre_channel;";
			//echo $sql_update_article_channel;
			$result_update_article_channel=mysql_query($sql_update_article_channel) or die(mysql_error());
		}
	 
		if($pre_channel!=0){
			/*更新父栏目is_bottom*/
			$sql_update_pre_channel="update channel set is_bottom=0 where channel_id=$pre_channel;";
			//echo $sql_update_pre_channel;
			$result_update_pre_channel=mysql_query($sql_update_pre_channel) or die(mysql_error());
		}
		echo "<script language=JavaScript> alert('添加栏目成功！'); window.location.href='channel_add.php';</script>";
	}
}